using DiLu.Expansion.File.Operates.FTP.Configs;

namespace DiLu.Expansion.File.Operates.FTP;

/// <summary>
///     获取FTP文件
/// </summary>
public static class GetFtpFile
{
    /// <summary>
    ///     获取FTP文件流
    /// </summary>
    /// <param name="config">获取FTP文件流配置</param>
    /// <returns></returns>
    public static async Task<Stream> GetAsync(this GetFtpFileStreamConfig config)
    {
        if (!config.FtpClient.IsConnected) await config.FtpClient.AutoConnect();
        var fileStream = new MemoryStream();
        await config.FtpClient.DownloadStream(fileStream, config.FileFullPath);
        fileStream.Position = 0;
        //不自动关闭客户端直接返回
        if (!config.AutoCloseFtpClient) return fileStream;
        await config.FtpClient.CloseFtpClientConnectAsync();
        return fileStream;
    }
}